#include "eventdetaildialog.h"
#include "ui_eventdetaildialog.h"
#include "databasemanager.h"
#include "session.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDateTime>
#include <QMessageBox>
#include <QPainter>
eventdetaildialog::eventdetaildialog(int eventId,
                                     const QString &name,
                                     const QString &type,
                                     const QString &startDate,
                                     const QString &endDate,
                                     const QString &description,
                                     bool ticketRequired,
                                     QWidget *parent) :
    QDialog(parent),
    ui(new Ui::eventdetaildialog),
    m_eventId(eventId)
{
    ui->setupUi(this);

    // 设置窗口背景色
    this->setStyleSheet("background-color: #f0f3f7;");

    // 左侧信息区卡片样式
    ui->widget->setStyleSheet(R"(
        QWidget {
            background-color: rgba(255, 255, 255, 220);
            border-radius: 15px;
            padding: 20px;
        }
    )");

    // 左侧标签样式统一
    QString labelStyle = R"(
        QLabel {
            font-size: 16px;
            color: #2c3e50;
            font-weight: 600;
            margin-bottom: 12px;
        }
    )";
    ui->labelName->setStyleSheet(labelStyle);
    ui->labelType->setStyleSheet(labelStyle);
    ui->labelStartDate->setStyleSheet(labelStyle);
    ui->labelEndDate->setStyleSheet(labelStyle);
    ui->labelTicketRequired->setStyleSheet(labelStyle);
    setWindowTitle(name);
    // 右上“活动介绍”标题样式
    ui->label_6->setStyleSheet(R"(
        QLabel {
            font-size: 18px;
            font-weight: 700;
            color: #34495e;
        }
    )");

    // 活动介绍 QTextBrowser 样式
    ui->textBrowserDescription->setStyleSheet(R"(
        QTextBrowser {
            background-color: rgba(255, 255, 255, 230);
            border-radius: 12px;
            border: 1px solid #d1d9e6;
            font-size: 14px;
            color: #34495e;
            padding: 15px;
        }
    )");

    // 预约按钮样式
    ui->pushButton1->setStyleSheet(R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #f39c12, stop:1 #d68910);
            color: white;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 600;
            padding: 8px 24px;
            border: none;
            min-width: 80px;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #f5b041, stop:1 #d18e10);
        }
        QPushButton:pressed {
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                stop:0 #d68910, stop:1 #b46d0e);
        }
    )");

    // 设置显示内容
    ui->labelName->setText(QString("活动名称：%1").arg(name));
    ui->labelType->setText(QString("活动类型：%1").arg(type));
    ui->labelStartDate->setText(QString("开始日期：%1").arg(startDate));
    ui->labelEndDate->setText(QString("结束日期：%1").arg(endDate));
    ui->labelTicketRequired->setText(QString("是否需要门票：%1").arg(ticketRequired ? "是" : "否"));
    ui->textBrowserDescription->setText(description);
}
void eventdetaildialog::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap background("C:/Users/m1937/Pictures/Screenshots/bcv.png");
    if (!background.isNull()) {
        painter.drawPixmap(this->rect(), background);  // 自动拉伸填充整个窗口区域
    } else {
        // 备用：背景图加载失败时，填充默认颜色
        painter.fillRect(this->rect(), Qt::white);
    }
    QDialog::paintEvent(event);
}
eventdetaildialog::~eventdetaildialog()
{
    delete ui;
}

void eventdetaildialog::on_pushButton1_clicked()
{
    int visitorId = Session::getVisitorId();
    if (visitorId < 0) {
        QMessageBox::warning(this, "错误", "未登录或用户信息异常，无法预约。");
        return;
    }

    QSqlDatabase db = DatabaseManager::instance().getDatabase();
    if (!db.isOpen()) {
        QMessageBox::warning(this, "错误", "数据库未连接，无法预约。");
        return;
    }

    QSqlQuery query(db);
    query.prepare("INSERT INTO Venue_Visits (venue_id, visitor_id, visit_date, visit_time, event_id) "
                  "VALUES ((SELECT venue_id FROM Exhibitions_Events WHERE event_id = :eventId), "
                          ":visitorId, :visitDate, :visitTime, :eventId);");
    query.bindValue(":eventId", m_eventId);
    query.bindValue(":visitorId", visitorId);

    QDateTime now = QDateTime::currentDateTime();
    query.bindValue(":visitDate", now.date().toString("yyyy-MM-dd"));
    query.bindValue(":visitTime", now.time().toString("HH:mm:ss"));

    if (!query.exec()) {
        QMessageBox::warning(this, "预约失败", "预约活动失败: " + query.lastError().text());
        return;
    }

    QMessageBox::information(this, "预约成功", "您已成功预约该活动！");
    this->accept();
}
